<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-data">&nbsp;&nbsp;财务管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-circle-plus">&nbsp;&nbsp;工资核算添加</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div>
      <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
        <el-divider content-position="left">数据添加</el-divider>
        <el-form
            :model="PayForm"
            :rules="rules"
            ref="PayForm"
            label-width="100px"
            style="margin-right: 25px">
          <el-row>
            <el-col :span="8">
              <el-form-item label="月份:" prop="month">
                <el-select v-model="PayForm.month" placeholder="请选择月份">
                  <el-option
                      v-for="item in months"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="姓名:" prop="name">
                <el-input v-model="PayForm.name" placeholder="输入姓名"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="性别:" prop="gender">
                <el-select v-model="PayForm.gender" placeholder="请选择性别">
                  <el-option
                      v-for="item in genders"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="职员ID:" prop="employeeId">
                <el-input v-model="PayForm.employeeId" placeholder="输入职员ID"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="入职时间:" prop="hiredate">
                <el-date-picker
                    v-model="PayForm.hiredate"
                    type="date"
                    format="yyyy 年 MM 月 dd 日"
                    value-format="yyyy-MM-dd"
                    placeholder="选择入职时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门名称:" prop="deptName">
                <el-input v-model="PayForm.deptName" placeholder="请输入部门名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="工作职位:" prop="job">
                <el-input v-model="PayForm.job" placeholder="请输入工作职位"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="调休天数:" prop="compensatoryDays">
                <el-input v-model="PayForm.compensatoryDays" placeholder="请输入调休天数"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="出勤天数:" prop="attendanceDays">
                <el-input v-model="PayForm.attendanceDays" placeholder="请输入出勤天数"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="基本工资:" prop="baseFunds">
                <el-input v-model="PayForm.baseFunds" placeholder="请输入基本工资"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="岗位工资:" prop="postFunds">
                <el-input v-model="PayForm.postFunds" placeholder="请输入岗位工资"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="津贴:" prop="subsidy">
                <el-input v-model="PayForm.subsidy" placeholder="请输入津贴"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="公积金:" prop="publicAccumulationFunds">
                <el-input v-model="PayForm.publicAccumulationFunds" placeholder="请输入公积金"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="工龄工资:" prop="jobLifeFunds">
                <el-input v-model="PayForm.jobLifeFunds" placeholder="请输入工龄工资"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="工资克扣:" prop="lessFunds">
                <el-input v-model="PayForm.lessFunds" placeholder="请输入工资克扣"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="克扣备注:" prop="remarks">
            <el-input v-model="PayForm.remarks" placeholder="输入克扣备注"></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="20">
              <el-form-item label="工资合计:" prop="sumSalary">
                <el-input v-model="PayForm.sumSalary" placeholder="输入工资合计"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item style="float: right">
                <el-button type="primary"@click="compute" style="background-color: white;border-color: gray">
                  <i class="el-icon-thumb" style="color:gray;font-size: 100%">&nbsp;&nbsp;计算</i>
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item>
            <el-button type="primary" @click="submitForm('PayForm')" style="color: white">添加</el-button>
            <el-button type="warning" @click="resetForm('PayForm')" style="color: white">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      PayForm: {
        month: '二月',
        employeeId: '12345',
        name: '阿金',
        gender: '男',
        hiredate: '2022-10-28',
        deptName: "技术部",
        job: "Java程序员",
        /*工资核算天数为26-5*/
        compensatoryDays: '5',
        attendanceDays: '26',
        baseFunds: '7000',
        postFunds: '1200',
        subsidy: '500',
        publicAccumulationFunds: '800',
        jobLifeFunds: '500',
        lessFunds: '500',
        remarks: '玩手机',
        sumSalary: '',
      },
      genders:[
        {label:'男',value:'男'},
        {label:'女',value:'女'},
      ],
      months: [
        {label: '一月', value: '一月'},
        {label: '二月', value: '二月'},
        {label: '三月', value: '三月'},
        {label: '四月', value: '四月'},
        {label: '五月', value: '五月'},
        {label: '六月', value: '六月'},
        {label: '七月', value: '七月'},
        {label: '八月', value: '八月'},
        {label: '九月', value: '九月'},
        {label: '十月', value: '十月'},
        {label: '十一月', value: '十一月'},
        {label: '十二月', value: '十二月'},
      ],
      rules: {
        month: [
          {required: true, message: "请选择月份", trigger: 'change'},
        ],
        employeeId: [
          {required: true, message: "请输入职员ID", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        name: [
          {required: true, message: "请输入姓名", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        gender: [
          {required: true, message: "请选择性别", trigger: 'change'},
        ],
        hiredate: [
          {required: true, message: "请选择入职日期", trigger: 'change'},
        ],
        deptName: [
          {required: true, message: "请输入部门名称", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        job: [
          {required: true, message: "请输入工作职位", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        compensatoryDays: [
          {required: true, message: "请输入调休天数", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        attendanceDays: [
          {required: true, message: "请输入出勤天数", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        baseFunds: [
          {required: true, message: "请输入基本工资", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        postFunds: [
          {required: true, message: "请输入岗位工资", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        subsidy: [
          {required: true, message: "请输入津贴", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        publicAccumulationFunds: [
          {required: true, message: "请输入公积金", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        jobLifeFunds: [
          {required: true, message: "请输入工龄工资", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        lessFunds: [
          {required: true, message: "请输入工资克扣", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        sumSalary: [
          {required: true, message: "请输入工资合计", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        remarks: [
          {required: true, message: "请输入备注", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
      },
    }
  },
  methods: {
    compute() {
      let sumSalary =
          (
              parseInt(this.PayForm.baseFunds) +
              parseInt(this.PayForm.postFunds) +
              parseInt(this.PayForm.subsidy) +
              parseInt(this.PayForm.publicAccumulationFunds) +
              parseInt(this.PayForm.jobLifeFunds) -
              parseInt(this.PayForm.lessFunds)
          ) * (
              parseInt(this.PayForm.attendanceDays) -
              parseInt(this.PayForm.compensatoryDays)
          ) / 30 ;
      this.$set(this.PayForm,'sumSalary',sumSalary)
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/financePayrolls/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.PayForm);
          console.log('formData = ' + formData);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log(responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加成功！',
                type: 'success'
              });
              location.reload();
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
}
</script>

<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

</style>